package com.shop.shopmall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shop.shopmall.pojo.Menu;
import com.shop.shopmall.service.MenuService;
import com.shop.shopmall.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 菜单管理模块
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    MenuService menuService;


    /**
     * 获取所有菜单
     * @return 返回所有菜单的集合
     */
    @GetMapping("/getAllMenus")
    public Result<?> getMenu() {
        List<Menu> menus = menuService.getAllMenus();
        return Result.success(menus);
    }


    /**
     * 根据名称获取菜单
     * @param menu 传入一个完成的对象 但只用到了名称获取
     * @return 名称LIKE的集合
     */
    @PostMapping
    public Result<?> getMenu(@RequestBody Menu menu) {
        return Result.success(menuService.getMenu(menu));
    }

    /**
     * 根据 ID 获取单个菜单
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> getMenuById(@PathVariable int id) {
        Menu menus = menuService.getById(id);
        return Result.success(menus);
    }

    /**
     * 添加新菜单
     * @param menu
     * @return
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody Menu menu) {
        menuService.save(menu);
        return Result.success();
    }

    /**
     * 更新菜单
     * @param menu
     * @return
     */
    @PutMapping
    public Result<?> update(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return Result.success();
    }

    /**
     *  删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable int id) {
        menuService.removeById(id);
        return Result.success();
    }

}
